// token
var jwt = require('koa-jwt')
var {secret} = require('../config')

var router = require('koa-router')()
router.prefix('/topics')
const {
    find,
    findById,
    checkOwner,
    create,
    update,
    remove,
    checkTopicExist,
    followTopic,
    unfollowTopic,
    listFollowers,
    listFollowingTopics,
    listQuestions
} = require('../controllers/topics')

// token认证中间件
var auth = jwt({secret}).unless({
    path: []
})

// 查询话题列表
router.get('/', find)
// 查询特定话题
router.get('/:id', findById)
// 创建话题
router.post('/', auth, create)
// 更新话题
router.patch('/:id', auth, checkTopicExist, checkOwner, update)
// 删除话题
// 注：最好不要删除，话题的关联比较广
router.delete('/:id', auth, checkTopicExist, checkOwner, remove)


// 关注话题
router.put('/following/:id', auth, checkTopicExist, followTopic)
// 取消关注话题
router.delete('/following/:id', auth, checkTopicExist, unfollowTopic)
// 获取话题的粉丝列表
router.get('/:id/followers', auth, checkTopicExist, listFollowers)
// 获取话题的问题列表
router.get('/:id/questions', listQuestions)

// 获取用户的关注话题列表
router.get('/:id/following', listFollowingTopics)

module.exports = router
